/**
 * 差旅统计控制类
 * @author zqd	差旅小组
 *
 * 下午08:28:42
 */
package com.cbts.action.show;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cbts.util.Highcharts;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.cbts.entity.CorporationMonthReport;
import com.cbts.entity.CorporationQuarterReport;
import com.cbts.entity.Department;
import com.cbts.entity.DepartmentMonthReport;
import com.cbts.entity.DepartmentQuarterReport;
import com.cbts.service.info.DepartmentService;
import com.cbts.service.show.ReportService;
import com.opensymphony.xwork2.ActionSupport;

/*
 * 页面上有三个div
 * 一个是公司报表，一个是部门报表，一个是节省费用报表
 */
public class ReportAction extends ActionSupport {
	private ReportService reportService;
	private DepartmentService departmentService;
	
	private String jsonResult;
	private Map<String, List<Year>> demoyearmap;	//部门月
	private Map<String, List<Year>> dequyearmap;	//部门季
	private List<Year> comoyearlist;				//公司月
	private List<Year> coquyearlist;				//公司季
	private List<Year> descyearlist;				//部门节省费
	private List<Department> delist;

	public void setReportSrevice(ReportService reportService) {
		this.reportService = reportService;
	}
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	


	
	public Map<String, List<Year>> getDemoyearmap() {
		return demoyearmap;
	}
	public void setDemoyearmap(Map<String, List<Year>> demoyearmap) {
		this.demoyearmap = demoyearmap;
	}
	public Map<String, List<Year>> getDequyearmap() {
		return dequyearmap;
	}
	public void setDequyearmap(Map<String, List<Year>> dequyearmap) {
		this.dequyearmap = dequyearmap;
	}
	public List<Year> getComoyearlist() {
		return comoyearlist;
	}
	public void setComoyearlist(List<Year> comoyearlist) {
		this.comoyearlist = comoyearlist;
	}
	public List<Year> getCoquyearlist() {
		return coquyearlist;
	}
	public void setCoquyearlist(List<Year> coquyearlist) {
		this.coquyearlist = coquyearlist;
	}
	public List<Department> getDelist() {
		return delist;
	}
	public void setDlist(List<Department> delist) {
		this.delist = delist;
	}
	public List<Year> getDescyearlist() {
		return descyearlist;
	}
	public void setDescyearlist(List<Year> descyearlist) {
		this.descyearlist = descyearlist;
	}
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String prepareSetting(){
		Map<String, List<Year>> map1 = new HashMap<String, List<Year>>();	//<部门名，年份>	月报
		Map<String, List<Year>> map2 = new HashMap<String, List<Year>>();	//<部门名，年份> 季报
		List<Year> list1 = new ArrayList<Year>();
		List<Year> list2 = new ArrayList<Year>();
		
		delist=departmentService.getAllDepartment();
		for(Department d:delist){
			List<Year> year1=new ArrayList<Year>();
			List<Year> year2=new ArrayList<Year>();
			List<String> myl=new ArrayList<String>();	//月
			List<String> qyl=new ArrayList<String>();	//季
			myl=reportService.getYear(0, d.getDepartmentId());
			qyl=reportService.getYear(1, d.getDepartmentId());
			for(int i=0;i<myl.size();i++){
				Year item=new Year(myl.get(i));
				year1.add(item);
			}
			map1.put(d.getDepartmentId(), year1);
			
			for(int i=0;i<qyl.size();i++){
				Year item=new Year(qyl.get(i));
				year2.add(item);
			}
			map2.put(d.getDepartmentId(), year2);
		}
		
		List<String> myl=new ArrayList<String>();
		List<String> qyl=new ArrayList<String>();
		myl=reportService.getYear(0, null);
		qyl=reportService.getYear(1, null);
		for(int i=0;i<myl.size();i++){
			Year item=new Year(myl.get(i));
			list1.add(item);
		}
		for(int i=0;i<qyl.size();i++){
			Year item=new Year(qyl.get(i));
			list2.add(item);
		}
		
		this.setDemoyearmap(map1);
		this.setDequyearmap(map2);
		this.setComoyearlist(list1);
		this.setCoquyearlist(list2);
		this.setDescyearlist(list1);
		return "preparesetting";
		
	}
	
	/**
	 * 获得公司报表数据
	 * @return
	 */
	public String getCoReport(){
		//前端获得请求参数（年份）
		//通过参数获得报表
		//将报表数据转换为json格式传回到前台
		HttpServletRequest request = ServletActionContext.getRequest();
		String year=request.getParameter("year");
		String type=request.getParameter("type");
		if(type.equals("0")){
			if(year==null){
				jsonResult="error";
			}else{
				List<CorporationMonthReport> cmlist=reportService.getCoMonthReport(year);
				if(!cmlist.isEmpty())
					jsonResult=convert(cmlist,2);
				else
					jsonResult="error";
			}
		}else if(type.equals("1")){
			if(year==null){
				jsonResult="error";
			}else{
				List<CorporationQuarterReport> cqlist=reportService.getCoQuarterReport(year);
				if(!cqlist.isEmpty())
					jsonResult=convert(cqlist,3);
				else
					jsonResult="error";
			}
		}
		ServletActionContext.getResponse().setContentType("text/xml");
		
		return SUCCESS;
	}
	
	/**
	 * 获得部门报表数据
	 * @return
	 */
	public String getDeReport(){
		//与getCoReport类似
		HttpServletRequest request = ServletActionContext.getRequest();
		String deid=request.getParameter("deid");
		String year=request.getParameter("year");
		String type=request.getParameter("type");
		if(type.equals("0")){
			if(deid==null||year==null){
				jsonResult="error";
			}else{
				List<DepartmentMonthReport> dmlist=reportService.getDeMonthReport(deid, year);
				if(!dmlist.isEmpty())	//不为空
					jsonResult=convert(dmlist,0);
				else 
					jsonResult="error";
			}
		}else if(type.equals("1")){
			if(deid==null||year==null){
				jsonResult="error";
			}else{
				List<DepartmentQuarterReport> dqlist=reportService.getDeQuarterReport(deid, year);
				if(!dqlist.isEmpty())	//不为空
					jsonResult=convert(dqlist,1);
				else
					jsonResult="error";
			}
		}
		ServletActionContext.getResponse().setContentType("text/xml");
		
		return SUCCESS;
	}
	
	/**
	 * 获得各部门每月节省费用的数据
	 * @return
	 */
	public String getSavingcostReport(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String year=request.getParameter("year");
		
		if(year==null){
			jsonResult="error";
		}else{
			Map<String,List<Double>> dscmap=reportService.getDeSavecostingReport(year);
			if(!dscmap.isEmpty()){
				List<Highcharts> data = new ArrayList<Highcharts>();
				JSONObject jsondata=new JSONObject();
				Iterator it=dscmap.keySet().iterator();
				while(it.hasNext()){
					Highcharts chart=new Highcharts();
					String name=(String)it.next();
					chart.setName(name);
					chart.setData(dscmap.get(name));
					data.add(chart);
				}
				jsondata.put("dataList", data);
				jsonResult=jsondata.toString();
			}else{
				jsonResult="error";
			}
		}
		ServletActionContext.getResponse().setContentType("text/xml");
		
		return SUCCESS;
	}
	
	private String convert(List list,int type){
		Highcharts apachart=new Highcharts();
		Highcharts aaachart=new Highcharts();
		Highcharts sachart=new Highcharts();
		List<Double> apalist=new ArrayList<Double>();
		List<Double> aaalist=new ArrayList<Double>();
		List<Double> salist=new ArrayList<Double>();
		List<Highcharts> dataList = new ArrayList<Highcharts>();
		JSONObject jsondata=new JSONObject();
		boolean sign;
		switch(type){
		case 0:			//部门月
			for(int i=0;i<12;i++){
				sign=false;
				for(int j=0;j<list.size();j++){
					DepartmentMonthReport dm=(DepartmentMonthReport)list.get(j);
					if(Integer.parseInt(dm.getId().getMonths().split("-")[1])==(i+1)){
						apalist.add(dm.getAllPreAmount());
						aaalist.add(dm.getAllAppAmount());
						salist.add(dm.getSaveAmount());
						sign=true;
						break;
					}
				}
				if(!sign){
					apalist.add(0.0);
					aaalist.add(0.0);
					salist.add(0.0);
				}
			}
			break;
		case 1:			//部门季
			for(int i=0;i<4;i++){
				sign=false;
				for(int j=0;j<list.size();j++){
					DepartmentQuarterReport dq=(DepartmentQuarterReport)list.get(j);
					if(Integer.parseInt(dq.getId().getQuarters().split("-")[1])==(i+1)){
						apalist.add(dq.getAllPreAmount());
						aaalist.add(dq.getAllAppAmount());
						salist.add(dq.getSaveAmount());
						sign=true;
						break;
					}
				}
				if(!sign){
					apalist.add(0.0);
					aaalist.add(0.0);
					salist.add(0.0);
				}
			}
			break;
		case 2:			//公司月
			for(int i=0;i<12;i++){
				sign=false;
				for(int j=0;j<list.size();j++){
					CorporationMonthReport cm=(CorporationMonthReport)list.get(j);
					if(Integer.parseInt(cm.getMonths().split("-")[1])==(i+1)){
						apalist.add(cm.getAllPreAmount());
						aaalist.add(cm.getAllAppAmount());
						salist.add(cm.getSaveAmount());
						sign=true;
						break;
					}
				}
				if(!sign){
					apalist.add(0.0);
					aaalist.add(0.0);
					salist.add(0.0);
				}
			}
			break;
		case 3:			//公司季
			for(int i=0;i<4;i++){
				sign=false;
				for(int j=0;j<list.size();j++){
					CorporationQuarterReport cq=(CorporationQuarterReport)list.get(j);
					if(Integer.parseInt(cq.getQuarters().split("-")[1])==(i+1)){
						apalist.add(cq.getAllPreAmount());
						aaalist.add(cq.getAllAppAmount());
						salist.add(cq.getSaveAmount());
						sign=true;
						break;
					}
				}
				if(!sign){
					apalist.add(0.0);
					aaalist.add(0.0);
					salist.add(0.0);
				}
			}
			break;
		}
		apachart.setName("总预算");
		apachart.setData(apalist);
		aaachart.setName("总报销费用");
		aaachart.setData(aaalist);
		sachart.setName("节省费用");
		sachart.setData(salist);
		
		dataList.add(apachart);
		dataList.add(aaachart);
		dataList.add(sachart);
		jsondata.put("dataList", dataList);
		
		return jsondata.toString();	
	}
}
